<?php
class TemplateController extends Core_Controller
{
    public function indexAction()
    {
        if(count($this->_http->getArvs())>0) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        $this->_view->title = "Quản lý template";
        
        $modelTemplate = new Admin_Model_Template();
        $this->_view->list = $modelTemplate->getTemplates();
    }
     
    public function createAction()
    {
        if(count($this->_http->getArvs())>0) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        $this->_view->title = "Thêm template";
        
        //status  0: chua submit , 1: da submit va co loi , 2: submit thanh cong
        $status = 0;
        $message = '';
        $name = '';
        $path = '';
        $description = '';
        
        if(isset($_POST['nameTemplate']))
        {
            $name = trim($_POST['nameTemplate']);
            $path = trim($_POST['pathTemplate']);
            $description = trim($_POST['description']);
            $status = 1;
            
            if($name == "")
            {
               $message = " Chưa nhập tên template";    
            } 
            else
               if($path == "")
               {
                    $message = "Chưa nhập đường dẫn";  
               }
                else
                {
                    $modelTemplate = new Admin_Model_Template();
        			$ok = $modelTemplate->insertTemplate($name, $path, $description);
                 
                    if(! $ok )
                       $message = " Thêm template thất bại do lỗi hệ thống";
                    else
                    {
                        $status = 2;
                        $this->redirect('/admin/template');
                    }  
                }
        }
        
        $this->_view->status = $status;
        $this->_view->message = $message;
        $this->_view->input_name = $name;
        $this->_view->input_path = $path;
        $this->_view->input_description = $description; 
    }
      
    public function editAction()
    {
        if(count($this->_http->getArvs()) != 1 ) throw new Exception("404 not found",404);
        $id = $this->_http->getParam('id');
        if($id == null) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        $this->_view->title = "Sửa template";
        
        //status  0: chua submit , 1: da submit va co loi , 2: submit thanh cong
        $status = 0;
        $message = '';
        $name = '';
        $path = '';
        $description = '';
        
        if(isset($_POST['nameTemplate']))
        {
            $name = trim($_POST['nameTemplate']);
            $path = trim($_POST['pathTemplate']);
            $description = trim($_POST['description']);
            $status = 1;
            
            if($name == "")
            {
               $message = " Chưa nhập tên template";    
            } 
            else
               if($path == "")
               {
                    $message = "Chưa nhập đường dẫn";  
               }
                else
                {
                    $modelTemplate = new Admin_Model_Template();
        			$ok = $modelTemplate->updateTemplate($id,$name, $path, $description);
                 
                    if(! $ok )
                       $message = " Sửa template thất bại do lỗi hệ thống";
                    else
                    {
                        $status = 2;
                        $this->redirect('/admin/template');
                    }  
                }
        }
        else
        {
            $modelTemplate = new Admin_Model_Template();
            $result = $modelTemplate->getTemplate($id);
            if($result == null) throw new Exception("404 not found",404);
            $name = $result[0]['template_name'];
            $path = $result[0]['template_path'];
            $description = $result[0]['description'];
        }
        
        $this->_view->status = $status;
        $this->_view->message = $message;
        $this->_view->input_name = $name;
        $this->_view->input_path = $path;
        $this->_view->input_description = $description;
        $this->_view->id   = $id; 
    }
    
    public function deleteAction()
    {
        if(count($this->_http->getArvs()) != 1 ) throw new Exception("404 not found",404);
        $id = $this->_http->getParam('id');
        if($id == null) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        
        $modelTemplate = new Admin_Model_Template();
        $ok = $modelTemplate->deleteTemplate($id);
        $this->redirect('/admin/template');
    }
}